package com.tech.koufu.ui.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.tech.koufu.R;
import com.tech.koufu.model.MarketStock;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter; 
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 行情设配器
 * @author 123
 *
 */
public class MarketAdapter extends BaseAdapter {
	public Context context;  
	public LayoutInflater mInflater = null;
	public ArrayList<MarketStock> datas;
	
	public MarketAdapter(Context context){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		datas = new ArrayList<MarketStock>();
	}

	@Override
	public int getCount() { 
		if(datas!=null){
			return datas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) { 
		return null;
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_market1, parent,false);  
             holder.txt_stock_name = (TextView)convertView.findViewById(R.id.txt_stock_name);
             holder.txt_stock_code = (TextView)convertView.findViewById(R.id.txt_stock_code);
             holder.txt_stock_price = (TextView)convertView.findViewById(R.id.txt_stock_price);
             holder.txt_stock_increment = (TextView)convertView.findViewById(R.id.txt_stock_increment); 
             
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         holder.txt_stock_name.setText(datas.get(position).stock_name);
         holder.txt_stock_code.setText(""+datas.get(position).stock_code);
         DecimalFormat decimalFormat=new DecimalFormat("##0.00");
         Float old_price = Float.valueOf(datas.get(position).old_price); 
         Float new_price = Float.valueOf(datas.get(position).new_price);
         String increment = decimalFormat.format(((new_price - old_price)/old_price *100));
         Log.w("increment", ""+increment);
         
         holder.txt_stock_price.setText(" "+new_price);
         holder.txt_stock_increment.setText(increment + "%");
         
//         SpannableStringBuilder style=new SpannableStringBuilder("人数：123456");          
//         style.setSpan(new ForegroundColorSpan(R.color.bgColor_6E),0,3,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
//         holder.txt_compe_participates.setText(style);  
		return convertView;
	}
	static class ViewHolder  
    {    
        public TextView txt_stock_name;  
        public TextView txt_stock_code;  
        public TextView txt_stock_price;  
        public TextView txt_stock_increment;   
        
    }  
}   
